﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nop.Core.Domain.Media;

namespace Nop.Services.Media
{
    /// <summary>
    /// image url help class
    /// </summary>
    public class ImageUrlUtil
    {
        public const string PictureUrl = "{0}{1}/{2}/{3}";

        public const string SeoImageNameFormat = "{0}_{1}{2}.{3}";
        public const string WatermarkSeoImageNameFormat = "{0}_{1}{2}_mark.{3}";
        public const string ThumbImageNameFormat = "{0}_{1}.{2}";
        public const string WatermarkThumbImageNameFormat = "{0}_{1}_mark.{2}";

        public static string GetImageAccessUrl(string fileName, bool? useSSL, string imageHost, string containerName)
        {
            var protocol = useSSL.HasValue ? (useSSL.Value ? "https://" : "http://") : "http://";
            return string.Format(PictureUrl, protocol, imageHost, containerName, fileName);
        }

        public static string GetImageFileName(Picture picture, int size = 0, bool isWaterMark = false)
        {
            var pictureId = FormatId(picture.Id);
            if (!String.IsNullOrEmpty(picture.FileName))
                pictureId = picture.FileName;

            var seoName = picture.SeoFilename;
            string localFilename;
            var lastPart = GetFileExtensionFromMimeType(picture.MimeType);

            if (string.IsNullOrEmpty(seoName))
                localFilename = isWaterMark
                    ? GetPictureNameNoSeo(WatermarkThumbImageNameFormat, pictureId, 0, lastPart)
                    : GetPictureNameNoSeo(ThumbImageNameFormat, pictureId, 0, lastPart);
            else
                localFilename = isWaterMark
                    ? GetPictureNameWithSeo(WatermarkSeoImageNameFormat, pictureId, seoName, 0, lastPart)
                    : GetPictureNameWithSeo(SeoImageNameFormat, pictureId, seoName, 0, lastPart);

            return localFilename;
        }

        public static string GetThumbImageFileName(Picture picture, int targetSize)
        {
            var lastPart = GetFileExtensionFromMimeType(picture.MimeType);
            
            string thumbFilename;

            if (!String.IsNullOrEmpty(picture.FileName))
            {
                thumbFilename = !String.IsNullOrEmpty(picture.SeoFilename)
                     ? GetPictureNameWithSeo(SeoImageNameFormat, picture.FileName, picture.SeoFilename, targetSize, lastPart)
                     : GetPictureNameNoSeo(ThumbImageNameFormat, picture.FileName, targetSize, lastPart);
            }
            else
            {
                var id = FormatId(picture.Id);
                thumbFilename = !String.IsNullOrEmpty(picture.SeoFilename)
                     ? GetPictureNameWithSeo(SeoImageNameFormat, id, picture.SeoFilename, targetSize, lastPart)
                     : GetPictureNameNoSeo(ThumbImageNameFormat, id, targetSize, lastPart);
            }

            return thumbFilename;
        }

        public static string GetPictureNameNoSeo(string formPart, string id, int size, string lastPart)
        {
            return size > 0 ? string.Format(formPart, id, "_" + size, lastPart) : string.Format(formPart, id, "", lastPart);
        }

        public static string GetPictureNameWithSeo(string formPart, string id, string seoName, int size, string lastPart)
        {
            return size > 0 ? string.Format(formPart, id, seoName, "_"+size, lastPart) : string.Format(formPart, id, seoName, "", lastPart);            
        }

        /// <summary>
        /// Returns the file extension from mime type.
        /// </summary>
        /// <param name="mimeType">Mime type</param>
        /// <returns>File extension</returns>
        public static string GetFileExtensionFromMimeType(string mimeType)
        {
            if (mimeType == null)
                return null;

            string[] parts = mimeType.Split('/');
            string lastPart = parts[parts.Length - 1];
            switch (lastPart)
            {
                case "pjpeg":
                    lastPart = "jpeg";
                    break;
                case "x-png":
                    lastPart = "png";
                    break;
                case "x-icon":
                    lastPart = "ico";
                    break;
            }
            return lastPart;
        }

        public static string FormatId(int id)
        {
            return id.ToString("0000000");
        }


    }
}
